package com.example.e_comm.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.e_comm.entity.Role;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface RoleMapper  extends BaseMapper<Role>{
    /**
     * 根据用户ID获取角色列表
     * @param userId 用户ID
     * @return 角色列表
     */
    @Select("SELECT * FROM roles r"
            + "INNER JOIN user_role ur ON r.id = ur.role_id"
            + "WHERE ur.user_id = #{userId} AND r.deleted =0")
    List<Role> getRolesByUserId(@Param("userId") Long userId);
    /**
     * 根据角色ID获取角色列表和权限列表
     * @param roleId 角色ID
     * @return 符合条件的角色列表和权限列表
     */
    @Select("SELECT * FROM roles r "
            +"INNER JOIN role_permission rp ON r.id = rp.role_id"
            +"INNER JOIN permissions p ON rp.perm_id = p.id"
            +"WHERE r.id = #{roleId} AND r.deleted = 0 ")
    Role getRoleWithPermissions(@Param("roleId") Long roleId);


}
